package io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
 * 	一个socket可以开启多个输出流，多线程输出，不会错乱。
 * 	多个线程使用一个io流，不会错乱。
 * @author 风蚕
 *
 */
public class OneSocketMoreIO {
	public static void main(String[] args) {
		Socket socket = null;
		try {
			socket = new Socket("127.0.0.1", 9100);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			InputStream is2 = socket.getInputStream();
			OutputStream os2 = socket.getOutputStream();
			
			
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						while(true) {
						try {
							StringBuffer str = new StringBuffer();
							for(int i=0;i<50;i++) {
								str.append("11111111111111111111111111111");
							}
							os.write(str.toString().getBytes());
							Thread.sleep(1444);
						} catch (IOException | InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						}
						
					}
				}).start();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						while(true) {
						try {
							StringBuffer str = new StringBuffer();
							for(int i=0;i<50;i++) {
								str.append("22222222222222222222222222222");
							}
							os.write(str.toString().getBytes());
							Thread.sleep(1770);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					}
				}).start();
				
				
				
			
		} catch (IOException  e) {
			e.printStackTrace();
		}
		
		
	}
}
